package com.wisedu.jwjob.util;

import com.wisedu.jwjob.bean.Order;
import com.wisedu.jwjob.bean.OrderDetail;
import org.nutz.json.Json;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @Author: hj
 * @Date: 2020/2/20 上午11:01
 */
public class ClassUtil {
    /**
     *
     * @param object   旧的对象带值
     * @param addMap   动态需要添加的属性和属性类型
     * @param addValMap  动态需要添加的属性和属性值
     * @return  新的对象
     * @throws Exception
     */
    public Object dynamicClass(Object object,HashMap addMap, HashMap addValMap) throws Exception {
        HashMap returnMap = new HashMap();
        HashMap typeMap = new HashMap();


        Class<?> type = object.getClass();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            System.out.println("propertyName====="+propertyName);
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(object);
                //可以判断为 NULL不赋值
                returnMap.put(propertyName, result);
                typeMap.put(propertyName, descriptor.getPropertyType());
            }
        }

        returnMap.putAll(addValMap);
        System.out.println("returnMap====="+Json.toJson(returnMap));
        typeMap.putAll(addMap);
        System.out.println("typeMap====="+Json.toJson(typeMap));
        //map转换成实体对象
        DynamicBean bean = new DynamicBean(typeMap);
        //赋值
        Set keys = typeMap.keySet();
        for (Iterator it = keys.iterator(); it.hasNext(); ) {
            String key = (String) it.next();
            bean.setValue(key, returnMap.get(key));
        }
        System.out.println("bean====="+Json.toJson(bean));
        Object obj = bean.getObject();
        return obj;
    }

    public static void main(String[] args) throws Exception {
        Order order = new Order();
        order.setId(1);
        order.setName("order1");
        List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderid(1);
        orderDetail.setOrderPrice("1USD");
        orderDetail.setOrderSku("Sku1");

        orderDetailList.add(orderDetail);

        OrderDetail orderDetail2 = new OrderDetail();
        orderDetail2.setOrderid(1);
        orderDetail2.setOrderPrice("2USD");
        orderDetail2.setOrderSku("Sku2");
        orderDetailList.add(orderDetail2);

        try {
            HashMap addMap = new HashMap();
            HashMap addValMap = new HashMap();
            addMap.put("orderDetail", Class.forName("java.util.List"));
            addValMap.put("orderDetail", orderDetailList);
            Object obj2= new ClassUtil().dynamicClass(order,addMap,addValMap);

            System.out.println(Json.toJson(obj2));
            // 通过反射查看所有方法名
            Class clazz = obj2.getClass();
            System.out.println("clazz======"+clazz.getName());
            Method[] methods = clazz.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                System.out.println(methods[i].getName()+":"+methods[i].invoke(obj2));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
 /*
        // 设置类成员属性
        HashMap propertyMap = new HashMap();

        propertyMap.put("id", Class.forName("java.lang.Integer"));

        propertyMap.put("name", Class.forName("java.lang.String"));

        propertyMap.put("address", Class.forName("java.lang.String"));

        // 生成动态 Bean
        DynamicBean bean = new DynamicBean(propertyMap);

        // 给 Bean 设置值
        bean.setValue("id", new Integer(123));

        bean.setValue("name", "454");

        bean.setValue("address", "789");

        // 从 Bean 中获取值，当然了获得值的类型是 Object

        System.out.println("  >> id      = " + bean.getValue("id"));

        System.out.println("  >> name    = " + bean.getValue("name"));

        System.out.println("  >> address = " + bean.getValue("address"));

        // 获得bean的实体
        Object object = bean.getObject();

        // 通过反射查看所有方法名
        Class clazz = object.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i].getName());
        }
        */
    }


}
